﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STS.Schema.Interfaces;
using STS.Schema.Enums;
using STS.Schema.Base;
using STS.Interfaces;

namespace STS.Schema.MSSql
{
    public class MSSqlColumn : SqlObjectBase, IColumn
    {
        [SqlQueryProperty("table_id")]
        public override int? ParentObjectID
        {
            get;
            internal set;
        }

        [SqlQueryProperty("column_name")]
        public override string Name
        {
            get;
            internal set;
        }

        public override SqlObjectType ObjectType
        {
            get { return SqlObjectType.Column; }
        }

        [SqlQueryProperty("collation_name")]
        public string Collation { get; internal set; } //collation_name

        [SqlQueryProperty("is_computed")]
        public bool Computed { get; internal set; } //is_computed

        [SqlQueryProperty("computed_text")]
        public string ComputedText { get; internal set; }    //computed_text

        [SqlQueryProperty("user_type_name")]
        public string TypeName { get; internal set; }//user_type_name

        [SqlQueryProperty("max_length")]
        public int? Length { get; internal set; }//max_length

        [SqlQueryProperty("precision")]
        public byte? Precision { get; internal set; }//precision

        [SqlQueryProperty("scale")]
        public byte? Scale { get; internal set; }//scale

        [SqlQueryProperty("default_value")]
        public string Default { get; internal set; } //default_value

        [SqlQueryProperty("column_id")]
        public int ID { get; internal set; } //column_id

        [SqlQueryProperty("is_identity")]
        public bool Identity { get; internal set; } //is_identity

        [SqlQueryProperty("identity_increment")]
        public string IdentityIncrement { get; internal set; } //identity_increment

        [SqlQueryProperty("identity_seed")]
        public string IdentitySeed { get; internal set; } //identity_seed

        [SqlQueryProperty("is_identity")]
        public bool InPrimaryKey { get; internal set; } //is_identity

        [SqlQueryProperty("is_foreign_key")]
        public bool IsForeignKey { get; internal set; } //is_foreign_key

        [SqlQueryProperty("is_full_text_indexed")]
        public bool IsFullTextIndexed { get; internal set; } //is_full_text_indexed

        [SqlQueryProperty("is_persisted")]
        public bool IsPersisted { get; internal set; } //is_persisted

        [SqlQueryProperty("is_identity_not_for_replication")]
        public bool NotForReplication { get; internal set; } //is_identity_not_for_replication

        [SqlQueryProperty("is_nullable")]
        public bool Nullable { get; internal set; } //is_nullable

        [SqlQueryProperty("is_rowguidcol")]
        public bool RowGuidCol { get; internal set; } //is_rowguidcol

        [SqlQueryProperty("rule_name")]
        public string Rule { get; internal set; } //rule_name

        [SqlQueryProperty("rule_schema")]
        public string RuleSchema { get; internal set; } //rule_schema

        [SqlQueryProperty("is_readonly")]
        public bool ReadOnly { get; internal set; } //true if parent is IView or column is computed, false otherwise

        public MSSqlColumn(ISchemaFactory schemaFactory, IDatabase database)
            : base(schemaFactory, database)
        {

        }


        public MSSqlColumn()
            : this(null, null)
        {

        }

        public override ISqlObjectCollection GetChildren<TChild>()
        {
            return null;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.Name);
            sb.Append(" (");
            if (this.InPrimaryKey)
            {
                sb.Append("PK, ");
            }
            if (this.IsForeignKey)
            {
                sb.Append("FK, ");
            }
            sb.AppendFormat("{0}, ", this.TypeName);
            if (this.Precision.HasValue || this.Scale.HasValue)
            {
                sb.Remove(sb.Length - 2, 2);
                sb.AppendFormat("[{0}{1}], ", this.Precision.HasValue ? this.Precision.Value + ", " : null, this.Scale.HasValue ? this.Scale : null);
            }
            else if (this.Length.HasValue)
            {
                sb.Remove(sb.Length - 2, 2);
                sb.AppendFormat("[{0}], ", this.Length);
            }
            if (this.Nullable)
            {
                sb.Append("null");
            }
            if (sb.ToString().EndsWith(", "))
            {
                sb.Remove(sb.Length - 2, 2);
            }
            sb.Append(")");
            return sb.ToString();
        }

    }
}
